package com.legendshop.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//@Component
//@ServletComponentScan
//@WebFilter(urlPatterns = "/*",filterName = "loginFilter")
public class LoginFilter implements Filter{
	/** The log. */
	private final static Logger log = LoggerFactory.getLogger(LoginFilter.class);
	
	private String encoding = "UTF-8";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    	HttpServletRequest request = (HttpServletRequest)servletRequest;
		request.setCharacterEncoding(this.encoding);
		servletResponse.setCharacterEncoding(this.encoding);
    	long t1 = System.currentTimeMillis();
    	filterChain.doFilter(servletRequest, servletResponse);
    	log.debug("Invoking action {}, cost times {}" ,request.getRequestURI(), (System.currentTimeMillis() - t1));
    }

    @Override
    public void destroy() {
    	log.debug("Login filter destroy");
    }
}
